package main

import (
	"bytes"
	"fmt"
	"github.com/go-kratos/kratos/pkg/log"
	"os/exec"
)

type EvalScriptsRequest struct {
	Scripts []string `json:"Scripts" form:"mobile" validate:"required"`
}

type EvalScriptsResponse struct {
	Outputs []Output `json:"outputs"`
}

type Output struct {
	Stdout string `json:"stdout"`
	Stderr string `json:"stderr"`
}

func main() {
	script:="/bin/ls"
	//args:=[] string{"/Users/tal/wangxiao/"}
	//args:=[] string{"/Users/tal/wangxiao/","|","grep","zeus-be"}
	//cmd := exec.Command(script, args...)
	cmd := exec.Command("bash","-c", "/bin/ls /Users/tal/wangxiao/ | grep zeus | grep be")
	var stdout, stderr bytes.Buffer
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr

	err := cmd.Run()
	if err != nil {
		log.Warn("run cmd[%s] failed with %s\n", script, err)
		stderrStr := string(stderr.Bytes())
		if stderrStr == "" {
			stderrStr = err.Error()
		}
		fmt.Println(stderrStr)
		return
	}

	fmt.Println(cmd.Stdout)
	return
}
